<!-- 问题列表信息 -->
<template>
  <div class="table">
    <el-tabs v-model="activeName" @tab-click="handleChangeTabs">
      <el-tab-pane label="问题列表" name="one">
        <SearchTable :problemClassAllList="problemClassAllList" :selectMap="selectMap" v-model:formStatus="formStatus" ref="searchTableRef" @handleAdd="handleAdd" @handleViewEdit="handleViewEdit" />
      </el-tab-pane>
      <el-tab-pane label="问题详情" name="two">
        <FormTab :problemClassAllList="problemClassAllList" :selectMap="selectMap" v-model:formStatus="formStatus" v-model:form="form" @handleInitData="clearData" @handleSuccess="handleSuccess" @handleFaqCategoryRefresh="handleGetFaqCategoryInfo" />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script setup lang="ts" name="commonProblemInfo">
import SearchTable from "@/components/configInfo/problemManage/commonProblemInfo/SearchTable.vue";
import FormTab from "@/components/configInfo/problemManage/commonProblemInfo/FormTab.vue";
import type { TabsMode, ViewMode, ViewModeFilter } from "@/types/common";
import type { TabsPaneContext } from "element-plus";
import type { IFaq } from "@/types/problemManage/faq";
import { getFaqCategoryInfo } from "@/utils/api/faqCategory";
import type { IFaqCategory, ISearchFaqCategory } from "@/types/problemManage/faqCategory";
import { useDictData } from "@/hooks/network/useDictData";
import type { IDictData } from "@/types/dict";

/* 表格状态 */
const formStatus = ref<ViewMode>("none");
/* 标签页切换 */
const activeName = ref<TabsMode>("one");
const problemClassAllList = ref<Array<IFaqCategory>>([]);
const selectMap = ref<Array<IDictData>>([]);
/* 表格子组件的引用 */
const searchTableRef = useTemplateRef("searchTableRef");

const form = reactive<IFaq>({
  id: 0,
  answer: "",
  category_id: "",
  category_scenario_key: "",
  question: "",
  sort: 0
});

/* 处理切换标签页 */
function handleChangeTabs(tab: TabsPaneContext) {
  if (tab.props.name === "two") {
    formStatus.value = "edit";
  } else if (tab.props.name === "one") {
    clearData();
  }
}

/* 清除数据 */
function clearData() {
  activeName.value = "one";
  formStatus.value = "none";
}

/* 处理修改成功 */
function handleSuccess() {
  searchTableRef.value?.getTableInfo();
}

/* 点击添加按钮 */
function handleAdd(code: ViewMode) {
  formStatus.value = code;
  activeName.value = "two";
}
/* 点击编辑按钮 */
function handleViewEdit(row: IFaq, code: ViewModeFilter) {
  formStatus.value = code;
  activeName.value = "two";
  /* nextTick 解决重置该表单项 初始化的数据问题 */
  nextTick(() => {
    form.id = row.id;
    form.category_id = row.category_id;
    console.log("form.category_id", form.category_id);

    form.category_scenario_key = row.category_scenario_key;
    form.question = row.question;
    form.answer = row.answer;
    form.sort = row.sort;
  });
}

/* 问题分类数据 */
function handleGetFaqCategoryInfo() {
  getFaqCategoryInfo<Array<IFaqCategory>, ISearchFaqCategory>()
    .then(res => {
      problemClassAllList.value = res.result;
    })
    .finally(() => {});
}

handleGetFaqCategoryInfo();
let { getDictData } = useDictData();
getDictData("FAQ_SCENARIO", selectMap);
</script>

<style lang="scss" scoped></style>
